#include "taskbar.h"
#include "winctl.h"
#include <stdio.h>
#include <unistd.h>
#include <sys/proc.h>
#include <sys/syscall.h>
#include <sys/ipc.h>

taskbar_t taskbar;

static void taskbar_msg(gui_spirit_t *spirit, uview_msg_t *msg);

static void power_btn_event()
{
    dprintf("[power] system ready off,please save data\n");
    syscall0(void,SYS_POWEROFF);
}

//taskbar widget corresponse
static int taskbar_func_init(gui_spirit_t *spirit)
{
    //create power button
    gui_spirit_t *power_btn=gui_button_create();
    if(!power_btn)
    {
        printf("[taskbar] create taskbar power button failed!\n");
        return -1;
    }
    power_btn->style.background_color=GUI_BLUE;
;
    gui_button_set_title(power_btn,"power");
    gui_spirit_reset_size(power_btn,spirit->height-2,spirit->height-2);
    gui_spirit_set_pos(power_btn,3,spirit->height/2-power_btn->height/2);

    gui_signal_connect(power_btn,"button_press",power_btn_event,NULL);

    gui_container_add(GUI_CONTAINER(spirit),power_btn);
}   

int taskbar_init()
{
    list_init(&taskbar.winctl_list_head);
    taskbar.taskbar_color = TASKBAR_COLOR_DEFAULT;
    taskbar.winctl_backcolor = WINCTL_BACKCOLOR;
    taskbar.winctl_activecolor = WINCTL_ACTIVECOLOR;
    taskbar.last_winctl = NULL;

    // create msg queue
    taskbar.icon_msgid = msgget(GUI_WINDOW_ICON_MSG_NAME, IPC_CREATE | IPC_EXCL);
    if (!taskbar.icon_msgid < 0)
    {
        printf("[taskbar] create taskbar message failed!\n");
        return -1;
    }

    gui_spirit_t *spirit = gui_window_create(GUI_WINDOW_POPUP);
    if (!spirit)
    {
        printf("taskbar: create window spirit failed!\n");
        return -1;
    }
    spirit->style.background_color = taskbar.taskbar_color;
    taskbar.spirit = spirit;

    gui_window_get_screen(GUI_WINDOW(spirit), &taskbar.screen_width, &taskbar.screen_height);
    gui_window_set_fixed(GUI_WINDOW(spirit), true);
    gui_window_set_default_size(GUI_WINDOW(spirit),taskbar.screen_width, TASKBAR_HEIGHT_DEFAULT);
    gui_window_set_resizeable(GUI_WINDOW(spirit),false);
    gui_window_set_position_absolute(GUI_WINDOW(spirit), 0, taskbar.screen_height - TASKBAR_HEIGHT_DEFAULT);
    gui_window_set_routine(GUI_WINDOW(spirit), taskbar_msg);
    gui_window_set_monitor(GUI_WINDOW(spirit), true);

    gui_rect_t rect = {0, 0, taskbar.screen_width, taskbar.screen_height - spirit->height};
    gui_window_set_maximum_rect(GUI_WINDOW(spirit), &rect);
    taskbar_drawbackground();

    if (!taskbar_clock_init(spirit) < 0)
        return -1;
    if(!taskbar_func_init(spirit)<0)
        return -1;

    gui_spirit_show_all(spirit);
    return 0;
}

void taskbar_exit()
{
    msgput(taskbar.icon_msgid);
    winctl_destroy_all();
    if (taskbar.spirit)
    {
        gui_spirit_destroy(taskbar.spirit);
        taskbar.spirit = NULL;
    }
    taskbar.screen_width = 0;
    taskbar.screen_height = 0;
}

void taskbar_drawbackground()
{
    // draw color
    gui_surface_t *surface = gui_window_get_surface(GUI_WINDOW(taskbar.spirit));
    gui_surface_rectfill(surface, 0, 0, surface->w,surface->h, taskbar.taskbar_color);
    gui_window_flip(GUI_WINDOW(taskbar.spirit));
}

//processing taskbar msg
static void taskbar_msg(gui_spirit_t *spirit, uview_msg_t *msg)
{
    int type = uview_msg_get_type(msg);
    int target = uview_msg_get_targe(msg);
    int id = uview_msg_get_id(msg);

    winctl_t *winctl;
    switch (id)
    {
    case UVIEW_MSG_CREATE:
        winctl_create(target);
        winctl_paint(NULL);
        gui_spirit_show(taskbar.clock_label);
        break;
    case UVIEW_MSG_CLOSE:
        if (type == UVIEW_TYPE_WINDOWS)
        {
            winctl = winctl_find(target);
            if (winctl)
            {
                winctl_destroy(winctl);
                winctl_paint(NULL); // paint all winctl
                gui_spirit_show(taskbar.clock_label);
            }
            taskbar.last_winctl = NULL; // clear last winctl
        }
        break;
    case UVIEW_MSG_ACTIVATE:
        if (type == UVIEW_TYPE_WINDOWS)
        {
            winctl = winctl_find(target);
            if (winctl)
            {
                winctl_get_focus(winctl);
                winctl_paint(winctl);
            }
        }
        break;
    case UVIEW_MSG_INACTIVATE:
        if (type == UVIEW_TYPE_WINDOWS)
        {
            winctl = winctl_find(target);
            if (winctl)
            {
                winctl_lose_focus(winctl);
                winctl_paint(winctl);
            }
        }
        break;

    case UVIEW_MSG_HIDE:
        if (type == UVIEW_TYPE_WINDOWS)
        {
            winctl = winctl_find(target);
            if (winctl)
            {
                winctl->ishidden = true;
                winctl_paint(winctl);
            }
        }
        break;
    case UVIEW_MSG_SHOW:
        if (type == UVIEW_TYPE_WINDOWS)
        {
            winctl = winctl_find(target);
            if (winctl)
            {
                winctl->ishidden = false;
                winctl_paint(winctl);
            }
        }
        break;
    case UVIEW_MSG_SETICON:
        if (type == UVIEW_TYPE_WINDOWS)
        {
            winctl = winctl_find(target);
            if (winctl && uview_msg_get_icontype(msg) == GUI_WINDOW_ICON_MIDDLE)
            {
                char icon_path[MAX_PATH_LEN+1];
                if (taskbar.icon_msgid >= 0)
                {
                    if (msgrecv(taskbar.icon_msgid, icon_path, MAX_PATH_LEN, 0) < 0)
                        strcpy(icon_path, WINCTL_ICON_PATH_DEFAULT);
                }
                else
                {
                    strcpy(icon_path, WINCTL_ICON_PATH_DEFAULT);
                }
                //set winctl icon accoring to window icon path
                winctl_set_icon(winctl, icon_path);
            }
        }
        break;
    default:
        break;
    }
}

